# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_realname=pupnp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.12.1
pkgrel=1
pkgdesc="Portable SDK for UPnP* Devices (mingw-w64)"
arch=('any')
url="http://pupnp.sourceforge.net/"
license=("BSD")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('strip' 'staticlibs')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/mrjimenez/pupnp/archive/release-${pkgver}.tar.gz
        002-libupnp-win32.patch
        005-backport-Fix-Windows-support-when-building-with-autotools.patch
        006-backport-mediatomb-fix-but-place-it-differently.patch
        007-backport-Fix-Windows-support-when-building-with-MinGW.patch
        008-backport-Use-int64_t-instead-of-long-long.patch
       )
sha256sums=('b4fc8f8e78d5d3259f321aecab1bc9d9ef7b9cea816c4b4d850b264120773290'
            '99a0334ff2dd01fdcbe37d7cfbbbc86dacbf2cb7cccd533be3c8833e4367af1c'
            '283ea618750121036195c3708183a45a1eff8f6f22698f8144e1e6688c509f54'
            'ce9d47c16f87fa8207cce3ae35bd8587ff7c16ce9f398ba60b002ec7f2049f05'
            'cdf00609cbf8e2b1de19e880c5d255d0a5ef8d4b4669eadb9b9b44ddcedf2a5f'
            '89baad80c8b308e58a46e8e0618ae555df0d47586d91592553742053e08e1303')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-release-${pkgver}"
  apply_patch_with_msg \
    002-libupnp-win32.patch \
    005-backport-Fix-Windows-support-when-building-with-autotools.patch \
    006-backport-mediatomb-fix-but-place-it-differently.patch \
    007-backport-Fix-Windows-support-when-building-with-MinGW.patch \
    008-backport-Use-int64_t-instead-of-long-long.patch

  autoreconf -fi
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  ../${_realname}-release-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared \
    --enable-ipv6

  make
}

package() {
  cd build-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install

  install -Dm644 ${srcdir}/${_realname}-release-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
